package com.yanqu.road.server.manger.activity.unionchest;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionChest.config.UnionChestBoxConfig;
import com.yanqu.road.entity.activity.unionChest.config.UnionChestConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGoodsType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.unionchest.UnionChestModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.property.Property;

import java.util.*;

public class UnionChestMgr extends TempMgr {

    private static UnionChestConfig config = null;

    /**
     * 重新加载活动
     */
    public static void reloadActivityData() {
        getLogger().info("reload UnionChestActivity start");
        List<ActivityInfo> list = NormalActivityMgr.getOpenActivityInfoList(eActivityType.UnionChest.getValue());
        if (list.isEmpty()) {
            config = null;
            getLogger().info("reload UnionChestActivity end, no activity in show time.");
            return;
        }
        ActivityInfo activityInfo = list.get(0);

        loadConfigFromDb(activityInfo);
        getLogger().info("reload UnionChestActivity end, activityId {}.", config.getActivityInfo().getActivityId());

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(UnionChestModule.class).userLogin();
        }
    }

    /**
     * 数据库加载配置
     */
    private static void loadConfigFromDb(ActivityInfo activityInfo) {
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
        UnionChestConfig tmpConfig = new UnionChestConfig(activityConfigMap);
        tmpConfig.setActivityInfo(activityInfo);

        config = tmpConfig;
    }

    /**
     * 拿配置
     */
    public static UnionChestConfig getConfig() {
        return config;
    }

    /**
     * 拿活动信息
     */
    public static ActivityInfo getActivityInfo() {
        return config == null ? null : config.getActivityInfo();
    }

    public static boolean hasActivity() {
        if (getActivityInfo() == null || config == null) {
            return false;
        }
        return true;
    }

    public static int getBoxGoodsId(int boxType){
        if (config== null){
            return 0;
        }
        UnionChestBoxConfig boxConfig = config.getBoxMap().get(boxType);
        if (null == boxConfig){
            return 0;
        }
        return boxConfig.getBoxId();
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static boolean isBigReward(int boxGoodsId, Property reward) {
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(boxGoodsId);
        if (null == goodsInfo) {
            return false;
        }
        if (goodsInfo.getType() != eGoodsType.UnionChest.getValue()){
            return false;
        }
        for (Integer goodsId : reward.getGoods().keySet()) {
            if (goodsInfo.isUnionChestBigReward(goodsId)){
                return true;
            }
        }
        return false;
    }
}
